
import { _decorator, Component, Node, Label, find } from 'cc';
import { BattleManager } from './BattleManager';
import { CommandType, Message, MessageType } from './framework/Message';
import { MessageBase } from './framework/MessageBase';
import { MessageCenter } from './framework/MessageCenter';
import { GameData } from './GameData';
import { Map } from './map/Map';
import { MiniMap } from './map/MiniMap';
import { Hero } from './player/Hero';
import { Item } from './player/Item';
import { Pet } from './player/Pet';
import { Utils } from './utils/Utils';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = EventManager
 * DateTime = Sat Feb 05 2022 15:05:56 GMT+0800 (China Standard Time)
 * Author = vvc_zeng
 * FileBasename = EventManager.ts
 * FileBasenameNoExtension = EventManager
 * URL = db://assets/script/map/EventManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('EventManager')
export class EventManager extends MessageBase {
    static Instance: EventManager = null;
    // 区域事件的id
    aid: number = -1;
    // 事件描述文本
    desc: Label;
    // 事件可能提供的子物品选项节点
    empty: Node[];
    // 事件的主要选项节点
    btns: Node[];
    onLoad() {
        super.onLoad()
        EventManager.Instance = this;

        this.desc = <Label>this.node.getChildByName("Desc").getComponent("cc.Label");
        this.empty = this.node.getChildByName("Empty").children;
        this.btns = [this.node.children[2], this.node.children[3], this.node.children[4]];
        console.log("BUTTON", this.btns);
    }
    init() {
        this.messageType = MessageType.TYPE_EVENT;
    }
    ReveiveMessage(message: Message) {
        super.ReveiveMessage(message);
        if (message.Command == CommandType.COM_EVENT_START) {
            this.eventHandler(message.Content)
        }
    }
    eventEnd() {
        // 不监听购买事件
        for (const n of this.empty) {
            n.off(Node.EventType.TOUCH_END);
        }
        // 不监听按钮事件
        for (const n of this.btns) {
            n.off(Node.EventType.TOUCH_END);
        }
        // 关闭事件显示区域
        this.node.active = false;
        // 返回地图中
        if (MiniMap.Instance.areaMapPos > -1) {
            console.log("返回小地图");
            MiniMap.Instance.node.active = true;
        } else {
            console.log("返回大地图");
            Map.Instance.node.active = true;
        }
        this.aid = -1;
    }
    // 游戏事件逻辑
    eventHandler(aid) {
        this.aid = Number(aid);
        // 显示事件区域
        this.node.active = true;
        // 显示事件描述信息
        this.desc.node.active = true;
        // 关闭地图显示，如果在小地图则关小地图
        if (MiniMap.Instance.areaMapPos > -1) {
            MiniMap.Instance.node.active = false;
        } else {
            Map.Instance.node.active = false;
        }

        // 判断藏宝图物品的延时触发条件
        Item.Instance.checkItem();

        this.desc.string = "这是一处" + GameData.Instance.areaTips[aid][0] + "," + GameData.Instance.areaTips[aid][1];
        // 根据 area id 处理不同的事件，用回调函数，弹出选项，让玩家决定
        // 后续再排除问题
        GameData.Instance.areaData[aid].func()
    }
    initButton(a: string, b?: string, c?: string) {
        (<Label>this.btns[0].getComponentInChildren("cc.Label")).string = a;
        this.btns[0].active = true;
        if (b) {
            this.btns[1].active = true;
            (<Label>this.btns[1].getComponentInChildren("cc.Label")).string = b;
        } else {
            this.btns[1].active = false;
        }
        if (c) {
            this.btns[2].active = true;
            (<Label>this.btns[2].getComponentInChildren("cc.Label")).string = c;
        } else {
            this.btns[2].active = false;
        }
    }
    buyCallback(arr) {
        let hero = Hero.Instance;
        if (hero.gold > 80) {
            if (arr[0] == 1) {
                let item = Item.Instance;
                item.getItem(arr[1]);
            } else {
                let pet = Pet.Instance;
                pet.getPet(arr[1]);
            }
            hero.gold -= 80;
        } else {
            console.log("金币不足");
        }
    }
    transport() {
        // 仅在大地图有传送区域
        Map.Instance.goNextFloor()
        Map.Instance.renderMapHints()
        this.eventEnd()
    }
    stayOrLeave() {
        // 2 种 躲避 停留
        this.initButton("躲避", "停留");
        this.btns[0].once(Node.EventType.TOUCH_END, () => {
            Hero.Instance.hp = Math.round(Hero.Instance.hp * 0.8);
            Hero.Instance.updateStat();
            this.eventEnd();
        }, this);
        this.btns[1].once(Node.EventType.TOUCH_END, () => {
            Hero.Instance.hp = Math.round(Hero.Instance.hp * 0.7);
            Hero.Instance.mp += 30;
            Hero.Instance.updateStat();
            this.eventEnd();
        }, this);
    }
    // 事件相关
    nearNewMap() {
        this.initButton("进入", "离开")
        this.btns[0].once(Node.EventType.TOUCH_END, () => {
            MessageCenter.SendCustomMessage(MessageType.TYPE_MINIMAP, CommandType.COM_MINIMAP_INIT);
            this.eventEnd();
        }, this);
        this.btns[1].once(Node.EventType.TOUCH_END, this.eventEnd, this);
    }
    meetGuard() {
        this.initButton("战斗", "交谈", "无视")
        this.btns[0].once(Node.EventType.TOUCH_END, () => {
            this.eventEnd();
            BattleManager.Instance.battleHandler(Utils.getRandomRange(0, 9));
        }, this);
        this.btns[1].once(Node.EventType.TOUCH_END, () => {
            Hero.Instance.hp += <number>Utils.getRandomRange(-10, 20);
            Hero.Instance.mp += <number>Utils.getRandomRange(-5, 10);
            Hero.Instance.updateStat();
            this.eventEnd();
        }, this);
        this.btns[2].once(Node.EventType.TOUCH_END, () => {
            Hero.Instance.gold += <number>Utils.getRandomRange(-20, 40);
            this.eventEnd()
        }, this);
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
